<?php
/*
通用分类
*/
class Con_sale extends Acan{
	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Acan_table_sale);
		if($_POST){Afun_cache_rm('Asale');}//有数据库操作删除导航缓存
	}
	function Act_search(){
		$key = $_POST['key'];
		$types = $_POST['type'];
		if(!empty($key)){
			if(is_numeric($key)){
				if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$key)){
					$wtype = "`type` = 'phone'";
				}elseif(strlen($key)==15){
					$wtype = "`type` = 'imei'";
				}else{
					$fuzzy = 1;
					$wtype = "(`type` = 'phone' or `type` = 'imei')";
				}
			}else{
				$wtype = "`type` = 'name'";
			}
			//--模糊查找
			if($fuzzy==1){
				$wdata = " and `data` LIKE '%".$key."%'";
			}else{
				$wdata = " and `data` = '".$key."'";
			}
			$con->where = $wtype.$wdata;
			$con->join = "LEFT JOIN `".Acan_table_asale_user."` u ON a.`uid`=u.`uid`";
			$this->userinfo = Afun_db()->model(Acan_table_asale_userinfo);
			$this->user = Afun_db()->model(Acan_table_asale_user);
			$data = $this->userinfo->findAll($con);
			if(empty($data)){
				if($fuzzy==1){
					$error = 1;
					$msg = '找不到!';
				}else{
					$msg = '是否新建用户?';
				}
			}else{
				$msg = '用户列表';
			}
		}else{
			$error = 1;
			$msg = '请填写内容!';
		}
		$json->error = $error;
		$json->msg = $msg;
		Afun_json($json);
	}
	//--下单
	function Act_orders(){
		$search_type = Afunc_enum()->data('asale_search_type');
		include $this->Atpl();
	}
	//--列表
	function Act_list(){
		if(empty($this->title)){$this->title = '销售列表';}
		$form_val_status = array('1'=>'开始交易','2'=>'交易中','3'=>'成功','4'=>'失败');
		//分页函数
	//	$con->where = "`pid`='$this->id'";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.status asc,a.date asc,a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$result = $this->model->findAll($con);
		foreach ($result as $key => $val) {
			if($_GET['search']){
				$val['name']=preg_replace("/($key)/i","<font color=red><b>\\1</b></font>",$val['name']);
			}
			$list[] = $val;
		}
		//获取父菜单信息
		$listp= $this->model->ofindPk($this->id);
		include $this->Atpl();
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		foreach($_POST['save'] as $i=>$vals){
			$this->model->updatePk($vals,$vals['id']);
		}
		Afun_refresh('保存成功','referrer');
	}
	//添加
	function Act_add(){
		Afun_post_auth();
		$_POST['add']['pid'] = $this->id;
		$_POST['add']['class'] = $_POST['add']['class']+1;
		if($this->model->save($_POST['add'])){
			Afun_refresh('添加成功','referrer');
		}else{Afun_refresh('添加失败','referrer');}
			
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	
}

?>